package com.jx.controller;

import com.jx.entity.User;
import com.jx.service.UserService;
import com.jx.utils.PageResult;
import com.jx.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.transaction.Transactional;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Mr.Liu
 * @Date 2021/11/1 14:49
 * @Version 1.0
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {

   @Resource
    private UserService userService;

    /**
     * 用户列表
     * @param model
     * @return
     */
    @RequestMapping(value = "/toUserListPage")
    public String toUserListPage(Model model){
        User user = userService.selectByPrimaryKey(1);
        System.out.println(user);
        model.addAttribute("user",user);
        return "user/userListPage";
    }

    /**
     * 获取列表数据
     * @return
     */
    @RequestMapping(value = "/getUserList")
    @ResponseBody
    public PageResult<User> getUserList(Integer page, Integer limit, String searchKey, String searchValue){
        if (page == null) {
            page = 0;
            limit = 0;
        }
        if (StringUtil.isBlank(searchValue)) {
            searchKey = null;
        }
        page = (page-1)*limit;
        PageResult<User> list = userService.getUserList(page, limit, searchKey, searchValue);

        return list;
    }

    /**
     * 根据id删除用户
     * @return
     */
    @RequestMapping(value = "/delUserById")
    @ResponseBody
    public Map<String,Object> delUserById(Integer id){
        Map<String,Object> map = new HashMap();
        int i = userService.deleteByPrimaryKey(id);
        if(i > 0){
            map.put("code",i);
            map.put("msg","删除成功");
        } else {
            map.put("code",i);
            map.put("msg","删除失败");
        }
        return map;
    }

    /**
     * 新增用户
     * @return
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Map<String,Object> add(User user){
        Map<String,Object> map = new HashMap();
        int i = userService.insert(user);
        if(i > 0){
            map.put("code",i);
            map.put("msg","添加成功");
        } else {
            map.put("code",i);
            map.put("msg","添加失败");
        }
        return map;
    }

    /**
     * 根据id修改用户
     * @return
     */
    @RequestMapping(value = "/edit")
    @ResponseBody
    public Map<String,Object> edit(User user){
        Map<String,Object> map = new HashMap();
        int i = userService.updateByPrimaryKeySelective(user);
        if(i > 0){
            map.put("code",i);
            map.put("msg","修改成功");
        } else {
            map.put("code",i);
            map.put("msg","修改失败");
        }
        return map;
    }


    /**
     * 用户详情
     * @param model
     * @return
     */
    @RequestMapping(value = "/userDetail")
    public String userDetail(Model model,HttpSession session){
        User user = (User)session.getAttribute("login_user");
        model.addAttribute("user",user);
        return "user/userDetail";
    }

    /**
     * 修改密码
     * @param model
     * @return
     */
    @RequestMapping(value = "/updatePass")
    public String updatePass(Model model,String id){
        model.addAttribute("id",id);
        return "user/updatePass";
    }

    /**
     * 事务测试
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/aopTest")
    public String aopTest(Model model,HttpSession session){
   userService.updateByPrimaryKeySelective2();
        return "";
    }

    /**
     * 无事务测试
     * @param model
     * @return
     */
    @RequestMapping(value = "/noAopTest")
    public String noAopTest(Model model,HttpSession session){
        User user = (User)session.getAttribute("login_user");
        model.addAttribute("user",user);
        return "user/userDetail";
    }


}
